package com.harmontronics.hanma.paas.paas.domain.iam.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.valueobject.HmActionFields;
import com.harmontronics.hanma.paas.paas.domain.iam.repository.persistence.HmActionRepository;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.HmAction;
import com.harmontronics.hanma.paas.paas.domain.iam.repository.po.HmPermActionPO;
import com.harmontronics.hanma.paas.paas.utils.MyBeanUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.harmontronics.hanma.paas.paas.common.PageBase;

import com.harmontronics.hanma.paas.paas.domain.iam.repository.mapper.HmActionMapper;
import com.harmontronics.hanma.paas.paas.domain.iam.repository.po.HmActionPO;
import java.util.List;

@Service("hmActionService")
@DS("#header.App-Unique")
public class HmActionService{

    @Autowired
    private HmActionRepository hmActionRepository;

    @Autowired
    private HmActionMapper hmActionMapper;

    @Autowired
    private HmPermActionService hmPermActionService;


    public HmAction findById( Integer id){
            HmActionPO po =   hmActionRepository.getById(id);
        return BeanUtil.toBean(po,HmAction.class);
    }

    public void update(HmAction hmAction){
            HmActionPO po =  BeanUtil.toBean(hmAction,HmActionPO.class);
            hmActionRepository.updateById(po);
    }

    public void remove(List<Long> idList){
        List<HmPermActionPO> permActions = hmPermActionService.getPermActions(idList);
        if (CollectionUtils.isNotEmpty(permActions)){
            //throw new ErrorKeyException(Constants.MODULE_ID + Errors.ERROR_CODE_3001);
        }

        hmActionRepository.removeByIds(idList);
    }

    public List<HmAction> getList(Long appId){
        QueryWrapper<HmActionPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(HmActionFields.APP_ID, appId);
        queryWrapper.orderByAsc(HmActionFields.ID);
        List<HmActionPO> poList = hmActionRepository.list(queryWrapper);
        List<HmAction> actionList = MyBeanUtils.batchTransform(HmAction.class,poList);
        return actionList;
    }

    public PageBase<HmAction> getList(int pageNo,int pageSize){
        Page<HmActionPO> page = new Page<>(pageNo, pageSize);
        IPage<HmActionPO> resultPage =
                    hmActionRepository.selectPage(Wrappers.emptyWrapper(),page);
        return PageBase.from(resultPage,HmAction.class);
    }

    public HmAction getAction(Long appId, String action) {
        QueryWrapper<HmActionPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(HmActionFields.APP_ID, appId);
        queryWrapper.eq(HmActionFields.ACTION, action);
        HmActionPO object = hmActionRepository.getOne(queryWrapper);
        return BeanUtil.toBean(object, HmAction.class);
    }

    public Long create(HmAction actionDto) {
        List<HmActionPO> actionList = hmActionMapper
                .selectList(
                        new QueryWrapper<HmActionPO>().eq(HmActionFields.APP_ID, actionDto.getAppId())
                                .eq(HmActionFields.ACTION, actionDto.getAction()));


        if (CollectionUtils.isNotEmpty(actionList)) {
            //throw new ErrorKeyException(Constants.MODULE_ID + Errors.ERROR_CODE_1028);
        }

        List<HmActionPO> nameActionList = hmActionMapper
                .selectList(
                        new QueryWrapper<HmActionPO>().eq(HmActionFields.APP_ID, actionDto.getAppId())
                                .eq(HmActionFields.NAME, actionDto.getName()));


        if (CollectionUtils.isNotEmpty(nameActionList)) {
            //throw new ErrorKeyException(Constants.MODULE_ID + Errors.ERROR_CODE_1029);
        }


        HmActionPO action = BeanUtil.toBean(actionDto, HmActionPO.class);

        hmActionMapper.insert(action);
        return action.getId();
    }

}